$(function(){
	$('#user').click(function(){
		location.href = '/signin'
	})
	$('#goBack').click(function(){
		history.go(-1)
	})
	
	//用户名验证
	  $('input[name="username"').blur(function(){
    	var $val = $(this).val()
    	if($val.length < 2 || $val.length > 16){
    		check(this)
    	}
    	else{
    		$.get('/username/'+$val,function(data){
    			check('input[name="username"',data.code)
    		})
    	}
    })
	
	//密码验证
  $('input[name="password"]').blur(function(){
  	var $val = $(this).val() 
  	if($val.length < 6 || $val.length > 16){
    		check(this)
    	}
    	else{
    		check(this,'success')
    	}
  })

   //确认密码验证
   $('input[name="repassword"]').blur(function(){
   	 var pass = $(':password').map(function(){
   	 	return $(this).val()
   	 	
   	 })
   	 //判断两次输入的密码是否相等
   	 if(pass[0] == pass[1]){
   	 	check(this,'success')
   	 }
   	 else{
   	 	check(this)
   	 }
   })
   
   
     $('form').submit(function(ev){
    	ev.preventDefault() 
    	var data = $(this).serialize()
    	$.post("/register",data,function(data){
    		$('.modal-body p').text(data.message)
    		$('.modal').modal('show')
    		if(data.code == 'success'){
    			$('.modal').on('hidden.bs.modal',function(e){
    				location.href="/signin"
    			})
    		}
    	})
    })
  
   
   
   
     function check(select,status){
    	status = status == 'success' ? 'has-success' : 'has-error'
    	$(select).parent().removeClass('has-error has-success').addClass(status)
    }
})
